#include <iostream>
#include <fstream>
#include <string>
#include "pb_cfg_fashion.pb.h"
#include "MsgFashion.pb.h"
#include "MsgPartnerInfo.pb.h"
#include "CTSizeString.h"
#include "MsgTips.pb.h"
#include "../mainctrl.h"

int CFashionMgr::LoadConfig(const char* szCfgPath)
{
    int iRet = 0;
    if (szCfgPath == NULL)
    {
        CT_ERROR(("Fashion Cfg Path is null"));
        return RET_SYSTEM_FAIL;
    }

    pb_cfg_fashion stFashionCfg;
    std::fstream stFileInput(szCfgPath, std::ios::in | std::ios::binary);
    if (!stFashionCfg.ParseFromIstream(&stFileInput))
    {
        CT_ERROR(("Failed to Parse FashionCfg!, %s", szCfgPath));
        return RET_SYSTEM_FAIL;
    }

    CFashionCfgUnit stCfgUnit;
    for (int i = 0; i < stFashionCfg.fashion_config_rows_size(); ++i)
    {
        const pb_cfg_fashion_pb_cfg_fashion_config_unit& stFashionUnit =
            stFashionCfg.fashion_config_rows(i);

        stCfgUnit.Clear();
        stCfgUnit._bHasConfig = false;
        stCfgUnit._usFashionId = stFashionUnit.id();
        stCfgUnit._usTargetId = stFashionUnit.target_id();
        stCfgUnit._ucPos = stFashionUnit.equip_pos();
        stCfgUnit._usValidDays = stFashionUnit.valid_days();
        stCfgUnit._stAttr.SetAttr(stFashionUnit.attr_type1(), stFashionUnit.attr_value1());
        stCfgUnit._stAttr.SetAttr(stFashionUnit.attr_type2(), stFashionUnit.attr_value2());
        stCfgUnit._stAttr.SetAttr(stFashionUnit.attr_type3(), stFashionUnit.attr_value3());
        stCfgUnit._stAttr.SetAttr(stFashionUnit.attr_type4(), stFashionUnit.attr_value4());
        stCfgUnit._stAttr.SetAttr(stFashionUnit.attr_type5(), stFashionUnit.attr_value5());
        stCfgUnit._stAttr.SetAttr(stFashionUnit.attr_type6(), stFashionUnit.attr_value6());
        stCfgUnit._usExtendDays = stFashionUnit.extend_days();
        stCfgUnit._iExtendGold = stFashionUnit.extend_gold();

        iRet = _stFashionCfg.AddConfig(stCfgUnit);
        if (iRet)
        {
            CT_ERROR(("%d, %d", stCfgUnit._usFashionId, iRet));
            return iRet;
        }
    }

    return 0;
}

int CFashionMgr::InitData(CUserFashion& stUserFashion,
        CPlayerOnlineData& stPlayerOnlineData)
{ 
    stPlayerOnlineData._uiNowWing = stUserFashion._uiNowWing;
    stPlayerOnlineData._uiNowDress = stUserFashion._uiNowDress;

    stPlayerOnlineData._ucFashionLevel = GetFashionMasterLevel(stUserFashion);

    if (stUserFashion._ucWingHidden)
    {
        stPlayerOnlineData._ucWingMode = 2;
    }
    else
    {
        stPlayerOnlineData._ucWingMode = 1;
    }

    stPlayerOnlineData._iWingEndTime = stUserFashion.GetWingEndTime(stUserFashion._uiNowWing);

    if (stUserFashion._ucDressHidden)
    {
        stPlayerOnlineData._ucDressMode = 2;
    }
    else
    {
        stPlayerOnlineData._ucDressMode = 1;
    }

    stPlayerOnlineData._iDressEndTime = stUserFashion.GetDressEndTime(stUserFashion._uiNowDress);

    return RET_OK;
}

void CFashionMgr::SendWingBagInfoToFront(CUserFashion& stUserFashion,
        unsigned int uiGID, PBMsgHead stMsgHead, CTLib::CEnpNetHead& stEnpHead)
{
    MsgAnsUserStorageInfo stAns;

    stMsgHead.set_msgid(ID_MAIN_GET_USER_STORAGE_INFO);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stAns.set_gid(uiGID);
    stAns.set_storage_type(STORAGE_TYPE_WING);

    stAns.set_grid_total_num(MAX_WING_BAG_SIZE);
    stAns.set_grid_default_num(MAX_WING_BAG_SIZE);
    stAns.set_grid_unlocked_num(0);

    for (int i = 0; i < stUserFashion._astWingBag.Size(); ++i)
    {
        StorageItemData & stStorageGrid = stUserFashion._astWingBag[i];

        MsgGridItemInfo* pstGridItemInfo = stAns.add_grid_items();

        pstGridItemInfo->set_item_store_pos(i);
        pstGridItemInfo->set_item_type(ITEM_TYPE_FASHION);
        pstGridItemInfo->set_item_count(1);
        pstGridItemInfo->set_item_config_id(stStorageGrid._uiItemID);
        pstGridItemInfo->set_invalid_time(stStorageGrid._iInvalidTime);
    }

    CMainCtrl::m_pstMain->m_stEncodeBuf.Clear();

    CMainCtrl::m_pstMain->EncodePBMsg(stMsgHead,
        CMainCtrl::m_pstMain->m_stEncodeBuf);

    CMainCtrl::m_pstMain->EncodePBMsg(stAns,
        CMainCtrl::m_pstMain->m_stEncodeBuf);

    CMainCtrl::m_pstMain->AnswerToClient(stEnpHead,
        CMainCtrl::m_pstMain->m_stEncodeBuf);
}

void CFashionMgr::SendDressBagInfoToFront(CUserFashion& stUserFashion,
        unsigned int uiGID, PBMsgHead stMsgHead, CTLib::CEnpNetHead& stEnpHead)
{
    MsgAnsUserStorageInfo stAns;

    stMsgHead.set_msgid(ID_MAIN_GET_USER_STORAGE_INFO);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stAns.set_gid(uiGID);
    stAns.set_storage_type(STORAGE_TYPE_DRESS);

    stAns.set_grid_total_num(MAX_DRESS_BAG_SIZE);
    stAns.set_grid_default_num(MAX_DRESS_BAG_SIZE);
    stAns.set_grid_unlocked_num(0);

    for (int i = 0; i < stUserFashion._astDressBag.Size(); ++i)
    {
        StorageItemData & stStorageGrid = stUserFashion._astDressBag[i];

        MsgGridItemInfo* pstGridItemInfo = stAns.add_grid_items();

        pstGridItemInfo->set_item_store_pos(i);
        pstGridItemInfo->set_item_type(ITEM_TYPE_FASHION);
        pstGridItemInfo->set_item_count(1);
        pstGridItemInfo->set_item_config_id(stStorageGrid._uiItemID);
        pstGridItemInfo->set_invalid_time(stStorageGrid._iInvalidTime);
    }

    CMainCtrl::m_pstMain->m_stEncodeBuf.Clear();

    CMainCtrl::m_pstMain->EncodePBMsg(stMsgHead,
        CMainCtrl::m_pstMain->m_stEncodeBuf);

    CMainCtrl::m_pstMain->EncodePBMsg(stAns,
        CMainCtrl::m_pstMain->m_stEncodeBuf);

    CMainCtrl::m_pstMain->AnswerToClient(stEnpHead,
        CMainCtrl::m_pstMain->m_stEncodeBuf);
}

int CFashionMgr::OpenFashionInBag(CPlayerBaseInfo& stPlayerBaseInfo,
        CUserFashion& stUserFashion, CPlayerOnlineData& stPlayerOnlineData,
        const CStorageGrid & stGrid)
{
    if (ITEM_TYPE_FASHION != stGrid._stKey._ucItemType)
    {
        CT_ERROR(("%d,%d,%d", stGrid._stKey._usStorePos,
            stGrid._stKey._ucItemType, stGrid._stKey._uiItemID));
        return ERROR_WRONG_ITEM_TYPE;
    }

    unsigned short usFashionID = (unsigned short)stGrid._stKey._uiItemID;
    if (!CMainCtrl::m_pstMain->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_26,
        stPlayerBaseInfo, stPlayerOnlineData))
    {
        return ERROR_SYS_NOT_OPEN;
    }

    int iRet = RET_OK;
    PBMsgHead stMsgHead;
    CEnpNetHead stNetHandle;
    CTSizeString<ROLENAME_LEN> szNameStr;
    iRet = CMainCtrl::m_pstMain->m_stPlayerOnline.GetOnlinePlayerHandle(
        stPlayerOnlineData._uiGID, stNetHandle);
    if (iRet)
    {
        CT_ERROR(("%d, %d", iRet, stPlayerBaseInfo._uiGID));
        return iRet;
    }
    
    //获取时装的配置
    CFashionCfgUnit stCfgData;
    iRet = _stFashionCfg.GetConfig(usFashionID, stCfgData);
    if (iRet)
    {
        CT_ERROR(("%d, %d", iRet, usFashionID));
        return iRet;
    }

    StorageItemData stItemData;
    stItemData._ucItemType = ITEM_TYPE_FASHION;
    stItemData._shItemCount = 1;
    stItemData._uiItemID = stCfgData._usFashionId;
    if (stCfgData._usTargetId > 0)
    {
        stItemData._uiItemID = stCfgData._usTargetId;
    }
    stItemData._iInvalidTime =
        (stCfgData._usValidDays*86400 +
            CTTimeUtils::GetDayBeginSec(CMainCtrl::m_pstMain->m_iNow));

    bool bIsNewFashion = true;
    if (stCfgData._ucPos == FASION_POS_WING)
    {
        for (int i = 0; i < stUserFashion._astWingBag.Size(); i++)
        {
            StorageItemData & stTmp = stUserFashion._astWingBag[i];
            if (stTmp._uiItemID == stItemData._uiItemID)
            {                    
                //更新有效期
                if (stTmp._iInvalidTime < CMainCtrl::m_pstMain->m_iNow)
                {
                    stTmp._iInvalidTime = stItemData._iInvalidTime;
                }
                else
                {
                    stTmp._iInvalidTime += stCfgData._usValidDays*86400;   
                }

                bIsNewFashion = false;
                break;
            }
        }

        if (bIsNewFashion)
        {
            if (stUserFashion._astWingBag.Size() ==
                    stUserFashion._astWingBag.GetCount())
            {
                return ERROR_FASION_WING_BAGFUL;
            }

            stUserFashion._astWingBag.AddOneItem(stItemData);
        }
        
        SendWingBagInfoToFront(stUserFashion, stPlayerBaseInfo._uiGID,
            stMsgHead, stNetHandle);             
    }
    else if (stCfgData._ucPos == FASION_POS_DRESS)
    {
        for (int i = 0; i < stUserFashion._astDressBag.Size(); i++)
        {
            StorageItemData & stTmp = stUserFashion._astDressBag[i];
            if (stTmp._uiItemID == stItemData._uiItemID)
            {                    
                //更新有效期
                if (stTmp._iInvalidTime < CMainCtrl::m_pstMain->m_iNow)
                {
                    stTmp._iInvalidTime = stItemData._iInvalidTime;
                }
                else
                {
                    stTmp._iInvalidTime += stCfgData._usValidDays*86400;   
                }

                bIsNewFashion = false;
                break;
            }
        }

        if (bIsNewFashion)
        {
            if (stUserFashion._astDressBag.Size() ==
                    stUserFashion._astDressBag.GetCount())
            {
                return ERROR_FASION_WING_BAGFUL;
            }

            stUserFashion._astDressBag.AddOneItem(stItemData);
        }
            
        SendDressBagInfoToFront(stUserFashion, stPlayerBaseInfo._uiGID,
            stMsgHead, stNetHandle);          
    }
    else
    {
        CT_ERROR(("%d, %d", stCfgData._ucPos, stPlayerBaseInfo._uiGID));
        return ERROR_FASION_CONFIG;
    }

    //取出物品
    iRet = CMainCtrl::m_pstMain->m_stStorageMgr.TakeOutItem(
        stPlayerOnlineData._stUserStorage, stGrid._stKey._usStorePos, 1);
    CHECK_RETURN(iRet);

    int iOldLevel = GetFashionMasterLevel(stUserFashion);
    if (bIsNewFashion)
    {
        stUserFashion._uiGainedFashionCnt += 1;
    }
    
    InitData(stUserFashion, stPlayerOnlineData);

    int iNewLevel = GetFashionMasterLevel(stUserFashion);
    if (iOldLevel != iNewLevel)
    {
        //时装大师的等级改变了, 通知用户属性变化
        CMainCtrl::m_pstMain->m_stNotifyMgr.Notify(stPlayerBaseInfo._uiGID,
            NOTIFY_ROLE_ATTR | NOTIFY_ROLE_INFO);

        MsgAnsFashionOpt stMsgAns;
        stMsgAns.set_opt(1);
        GetFashionInfo(stUserFashion, stPlayerOnlineData, stMsgAns);
        CMainCtrl::m_pstMain->NotifyOnlineUser(stPlayerBaseInfo._uiGID,
            ID_MAIN_FASHION_OPT, stMsgAns, false);

        if (iNewLevel >= ENM_FASHION_MACRO_MASTER_LVL)
        {
            MsgTips stTips;
            stTips.set_type(TIPS_FASHION_MASTER_LEVEL);
            stTips.add_tip_argument(stPlayerBaseInfo._sRoleName());
            szNameStr.Set("%d",iNewLevel);
            stTips.add_tip_argument(szNameStr());
            CMainCtrl::m_pstMain->SendOnlineTips(stTips);
        }
    }

    return RET_OK;
}

void CFashionMgr::GetFashionInfo(CUserFashion& stUserFashion,
        CPlayerOnlineData& stPlayerOnlineData, MsgAnsFashionOpt& stAns)
{
    stAns.set_wing_mode(stPlayerOnlineData._ucWingMode);
    stAns.set_dress_mode(stPlayerOnlineData._ucDressMode);
    stAns.set_now_wing_id(stPlayerOnlineData._uiNowWing);
    stAns.set_now_dress_id(stPlayerOnlineData._uiNowDress);
    stAns.set_master_level(stPlayerOnlineData._ucFashionLevel);
    stAns.set_master_ratio(GetFashionMasterAddtion(stUserFashion));
}

void CFashionMgr::GetFashionExtAttr(CUserFashion& stUserFashion,
        CPlayerOnlineData& stPlayerOnlineData, MsgFashionAttr& stFashionAttr)
{
	CPlayerAttr  stAttr;

	stFashionAttr.set_now_wing_id(stPlayerOnlineData._uiNowWing);
	stFashionAttr.set_now_dress_id(stPlayerOnlineData._uiNowDress);
    stFashionAttr.set_master_level(stPlayerOnlineData._ucFashionLevel);
	MsgRoleFightAttrs *pExtAttr = stFashionAttr.mutable_wing_attr();
	
	GetFashionWingAttr(stUserFashion, stAttr);
	CMainCtrl::m_pstMain->m_stRoleMgr.CopyExtAttr(stAttr, *pExtAttr);
	
	stAttr.Clear();
	pExtAttr = stFashionAttr.mutable_dress_attr();
	GetFashionDressAttr(stUserFashion, stAttr);
	CMainCtrl::m_pstMain->m_stRoleMgr.CopyExtAttr(stAttr, *pExtAttr);

}



int CFashionMgr::GetFashionMasterLevel(CUserFashion& stUserFashion)
{
    unsigned int uiCnt = stUserFashion._uiGainedFashionCnt;
    if (uiCnt < 2)
    {
        return 0;
    }

    if (uiCnt < 4)
    {
        return 1;
    }
    
    if (uiCnt < 7)
    {
        return 2;
    }

    if (uiCnt < 11)
    {
        return 3;
    }
    
    if (uiCnt < 17)
    {
        return 4;
    }

    if (uiCnt < 24)
    {
        return 5;
    }

    return 6;
}

int CFashionMgr::GetFashionMasterAddtion(CUserFashion& stUserFashion)
{
    unsigned int uiCnt = stUserFashion._uiGainedFashionCnt;
    if (uiCnt < 2)
    {
        return 0;
    }

    if (uiCnt < 4)
    {
        return 60;
    }

    if (uiCnt < 7)
    {
        return 120;
    }

    if (uiCnt < 11)
    {
        return 180;
    }

    if (uiCnt < 17)
    {
        return 240;
    }

    if (uiCnt < 24)
    {
        return 300;
    }

    return 360;
}

int CFashionMgr::GetFashionAttr(CUserFashion& stUserFashion,
        CPlayerAttr& stAttr)
{
    stAttr.Clear();
    
    if (stUserFashion._uiNowWing <= 0 && stUserFashion._uiNowDress <= 0)
    {
        return 0;
    }

    int iRet = RET_OK;
    int iAddtion = GetFashionMasterAddtion(stUserFashion);
    int64 i64RatioTmp = 0;

    for (int i = 0; i < stUserFashion._astWingBag.Size(); ++i)
    {
        StorageItemData & stItemData = stUserFashion._astWingBag[i];

        if (stUserFashion._uiNowWing != stItemData._uiItemID)
        {
            continue;
        }

        if (stItemData._iInvalidTime < CMainCtrl::m_pstMain->m_iNow)
        {
            break;
        }

        CFashionCfgUnit stCfgData;
        iRet = _stFashionCfg.GetConfig(stItemData._uiItemID, stCfgData);
        if (iRet)
        {
            CT_ERROR(("Wing(%u) Attr Get Config Error(%d)",
                stItemData._uiItemID, iRet));
            break;
        }

        if (iAddtion > 0)
        {
            for (int j = 0; j < stCfgData._stAttr._aiAttr.Size(); ++j)
            {
                if (stCfgData._stAttr._aiAttr[j] <= 0)
                {
                    continue;
                }
            
                i64RatioTmp =  INT64_1 * stCfgData._stAttr._aiAttr[j] *
                    (BATTLE_RATE_SKILL_PERCENT + iAddtion);
                i64RatioTmp /= BATTLE_RATE_SKILL_PERCENT;
                stAttr._aiAttr[j] += (int)i64RatioTmp;
            }
        }
        else
        {
            stAttr += stCfgData._stAttr;
        }
    }
    
    for (int i = 0; i < stUserFashion._astDressBag.Size(); ++i)
    {
        StorageItemData & stItemData = stUserFashion._astDressBag[i];

        if (stUserFashion._uiNowDress != stItemData._uiItemID)
        {
            continue;
        }

        if (stItemData._iInvalidTime < CMainCtrl::m_pstMain->m_iNow)
        {
            break;
        }

        CFashionCfgUnit stCfgData;
        iRet = _stFashionCfg.GetConfig(stItemData._uiItemID, stCfgData);
        if (iRet)
        {
            CT_ERROR(("Dress(%u) Attr Get Config Error(%d)",
                stItemData._uiItemID, iRet));
            break;
        }

        if (iAddtion > 0)
        {
            for (int j = 0; j < stCfgData._stAttr._aiAttr.Size(); ++j)
            {
                if (stCfgData._stAttr._aiAttr[j] <= 0)
                {
                    continue;
                }
            
                i64RatioTmp =  INT64_1 * stCfgData._stAttr._aiAttr[j] *
                    (BATTLE_RATE_SKILL_PERCENT + iAddtion);
                i64RatioTmp /= BATTLE_RATE_SKILL_PERCENT;
                stAttr._aiAttr[j] += (int)i64RatioTmp;
            }
        }
        else
        {
            stAttr += stCfgData._stAttr;
        }
    }
    
    return RET_OK;
}

int CFashionMgr::GetFashionWingAttr(CUserFashion& stUserFashion,
        CPlayerAttr& stAttr)
{
    stAttr.Clear();
    
    if (stUserFashion._uiNowWing <= 0)
    {
        return 0;
    }

    int iRet = RET_OK;
    int iAddtion = GetFashionMasterAddtion(stUserFashion);
    int64 i64RatioTmp = 0;

    for (int i = 0; i < stUserFashion._astWingBag.Size(); ++i)
    {
        StorageItemData & stItemData = stUserFashion._astWingBag[i];

        if (stUserFashion._uiNowWing != stItemData._uiItemID)
        {
            continue;
        }

        if (stItemData._iInvalidTime < CMainCtrl::m_pstMain->m_iNow)
        {
            break;
        }

        CFashionCfgUnit stCfgData;
        iRet = _stFashionCfg.GetConfig(stItemData._uiItemID, stCfgData);
        if (iRet)
        {
            CT_ERROR(("Wing(%u) Attr Get Config Error(%d)",
                stItemData._uiItemID, iRet));
            break;
        }

        if (iAddtion > 0)
        {
            for (int j = 0; j < stCfgData._stAttr._aiAttr.Size(); ++j)
            {
                if (stCfgData._stAttr._aiAttr[j] <= 0)
                {
                    continue;
                }
            
                i64RatioTmp =  INT64_1 * stCfgData._stAttr._aiAttr[j] *
                    (BATTLE_RATE_SKILL_PERCENT + iAddtion);
                i64RatioTmp /= BATTLE_RATE_SKILL_PERCENT;
                stAttr._aiAttr[j] += (int)i64RatioTmp;
            }
        }
        else
        {
            stAttr += stCfgData._stAttr;
        }

    }
#ifdef _DEBUG
       CT_BATTLE(("Wing(%u)翅膀属性加成:",stUserFashion._uiNowWing));
       CMainCtrl::m_pstMain->m_stRoleMgr.DumpOnlineRoleInfo(stAttr);
#endif

	return RET_OK;
}

int CFashionMgr::GetFashionDressAttr(CUserFashion& stUserFashion,
        CPlayerAttr& stAttr)
{
    stAttr.Clear();
    
    if (stUserFashion._uiNowDress <= 0)
    {
        return 0;
    }

    int iRet = RET_OK;
    int iAddtion = GetFashionMasterAddtion(stUserFashion);
    int64 i64RatioTmp = 0;

    for (int i = 0; i < stUserFashion._astDressBag.Size(); ++i)
    {
        StorageItemData & stItemData = stUserFashion._astDressBag[i];

        if (stUserFashion._uiNowDress != stItemData._uiItemID)
        {
            continue;
        }

        if (stItemData._iInvalidTime < CMainCtrl::m_pstMain->m_iNow)
        {
            break;
        }

        CFashionCfgUnit stCfgData;
        iRet = _stFashionCfg.GetConfig(stItemData._uiItemID, stCfgData);
        if (iRet)
        {
            CT_ERROR(("Dress(%u) Attr Get Config Error(%d)",
                stItemData._uiItemID, iRet));
            break;
        }

        if (iAddtion > 0)
        {
            for (int j = 0; j < stCfgData._stAttr._aiAttr.Size(); ++j)
            {
                if (stCfgData._stAttr._aiAttr[j] <= 0)
                {
                    continue;
                }
            
                i64RatioTmp =  INT64_1 * stCfgData._stAttr._aiAttr[j] *
                    (BATTLE_RATE_SKILL_PERCENT + iAddtion);
                i64RatioTmp /= BATTLE_RATE_SKILL_PERCENT;
                stAttr._aiAttr[j] += (int)i64RatioTmp;
            }
        }
        else
        {
            stAttr += stCfgData._stAttr;
        }
	}
#ifdef _DEBUG
       CT_BATTLE(("Wing(%u)时装属性加成:",stUserFashion._uiNowDress));
       CMainCtrl::m_pstMain->m_stRoleMgr.DumpOnlineRoleInfo(stAttr);
#endif


	return RET_OK;
}

int CFashionMgr::UseFashion(CUserFashion& stUserFashion,
        CPlayerOnlineData& stPlayerOnlineData, unsigned short usFashionID)
{
    int iNowTime = CMainCtrl::m_pstMain->m_iNow;
    for (int i = 0; i < stUserFashion._astWingBag.Size(); ++i)
    {
        StorageItemData & stItemData = stUserFashion._astWingBag[i];
        if (usFashionID == stItemData._uiItemID)
        {
            if (stItemData._iInvalidTime <= iNowTime)
            {
                return ERROR_FASION_EXPIRED;
            }

            stUserFashion._uiNowWing = usFashionID;
            stPlayerOnlineData._uiNowWing = usFashionID;

            return RET_OK;
        }
    }

    for (int i = 0; i < stUserFashion._astDressBag.Size(); ++i)
    {
        StorageItemData & stItemData = stUserFashion._astDressBag[i];
        if (usFashionID == stItemData._uiItemID)
        {
            if (stItemData._iInvalidTime <= iNowTime)
            {
                return ERROR_FASION_EXPIRED;
            }

            stUserFashion._uiNowDress = usFashionID;
            stPlayerOnlineData._uiNowDress = usFashionID;

            return RET_OK;
        }
    }

    return ERROR_FASION_NONE;
}

int CFashionMgr::HiddenFashion(CUserFashion& stUserFashion,
        CPlayerOnlineData& stPlayerOnlineData, unsigned char ucOpt)
{
    if (CMD_FASHION_SUB_SHOW_WING == ucOpt)
    {
        stUserFashion._ucWingHidden = false;
    }
    else if (CMD_FASHION_SUB_HIDE_WING == ucOpt)
    {
        stUserFashion._ucWingHidden = true;
    }
    else if (CMD_FASHION_SUB_SHOW_DRESS == ucOpt)
    {
        stUserFashion._ucDressHidden = false;
    }
    else if (CMD_FASHION_SUB_HIDE_DRESS == ucOpt)
    {
        stUserFashion._ucDressHidden = true;
    }
    else
    {
        return ERROR_FASION_OPT;
    }

    return RET_OK;
}

int CFashionMgr::ExtendValidity(CPlayerBaseInfo& stPlayerBaseInfo,
        CPlayerOnlineData& stPlayerOnline, CUserFashion& stUserFashion,
        unsigned short usFashionID, int& iFasionPos)
{
    CFashionCfgUnit stCfgData;
    int iRet = _stFashionCfg.GetConfig(usFashionID, stCfgData);
    if (iRet)
    {
        CT_ERROR(("%d, %d", iRet, usFashionID));
        return iRet;
    }

    if (stPlayerBaseInfo._iGold < stCfgData._iExtendGold)
    {
        return ERROR_GOLD_LESS;
    }    

    int iNowTime = CMainCtrl::m_pstMain->m_iNow;
    for (int i = 0; i < stUserFashion._astWingBag.Size(); ++i)
    {
        StorageItemData & stItemData = stUserFashion._astWingBag[i];
        if (usFashionID == stItemData._uiItemID)
        {
            if (stItemData._iInvalidTime <= iNowTime)
            {
                stItemData._iInvalidTime =
                    (CTTimeUtils::GetDayBeginSec(iNowTime) +
                        stCfgData._usExtendDays * 86400);
            }
            else
            {
                stItemData._iInvalidTime += stCfgData._usExtendDays * 86400;
            }

            if (stPlayerOnline._uiNowWing == stItemData._uiItemID)
            {
                stPlayerOnline._iWingEndTime = stItemData._iInvalidTime;
            }

            CMainCtrl::m_pstMain->m_stItemMgr.SubGold(stPlayerBaseInfo,
                OSS_MONEY_MODULE_54, stCfgData._iExtendGold);

            iFasionPos = FASION_POS_WING;

            return RET_OK;
        }
    }

    for (int i = 0; i < stUserFashion._astDressBag.Size(); ++i)
    {
        StorageItemData & stItemData = stUserFashion._astDressBag[i];
        if (usFashionID == stItemData._uiItemID)
        {
            if (stItemData._iInvalidTime <= iNowTime)
            {
                stItemData._iInvalidTime =
                    (CTTimeUtils::GetDayBeginSec(iNowTime) +
                        stCfgData._usExtendDays * 86400);
            }
            else
            {
                stItemData._iInvalidTime += stCfgData._usExtendDays * 86400;
            }

            if (stPlayerOnline._uiNowDress == stItemData._uiItemID)
            {
                stPlayerOnline._iDressEndTime = stItemData._iInvalidTime;
            }

            CMainCtrl::m_pstMain->m_stItemMgr.SubGold(stPlayerBaseInfo,
                OSS_MONEY_MODULE_54, stCfgData._iExtendGold);

            iFasionPos = FASION_POS_DRESS;

            return RET_OK;
        }
    }

    return ERROR_FASION_NONE;
}

bool CFashionMgr::HasThisFashion(unsigned short usFashionID)
{
    return  _stFashionCfg.IsValidFaionId (usFashionID);
}

int CFashionMgr::UnWareFashion(CUserFashion& stUserFashion,
        unsigned short usFashionID)
{
    CFashionCfgUnit stCfgData;
    int iRet = _stFashionCfg.GetConfig(usFashionID, stCfgData);
    if (iRet)
    {
        CT_ERROR(("%d, %d", iRet, usFashionID));
        return iRet;
    }

    if (stCfgData._ucPos == FASION_POS_WING &&
        usFashionID == stUserFashion._uiNowWing)
    {
        stUserFashion._uiNowWing = 0;        
        return RET_OK;
    }

    if (stCfgData._ucPos == FASION_POS_DRESS &&
        usFashionID == stUserFashion._uiNowDress)
    {
        stUserFashion._uiNowDress = 0;
        return RET_OK;
    }

    return ERROR_FASION_NONE;
}

